#include <S32FILE.H>
#include <CoeMain.h>
#include <uikon.hrh>
#include <APGCLI.H>

#include "AutolockSettings.h"
#include "Utils.h"
#include "Uids.h"

TAutolockSettings::~TAutolockSettings()
{
	iAppList.Close();
}

void TAutolockSettings::SaveSettingsL()
{
	CDictionaryStore *iniFile = CDictionaryFileStore::OpenLC(CCoeEnv::Static()->FsSession(), KSettingsFileName, KUidMyApp);

	Utils::SaveIntL(iniFile, EAutoLockEnabled, iAutoLockEnabled );
	Utils::SaveIntL(iniFile, ECustomProfiles, iCustomProfiles );
	Utils::SaveIntL(iniFile, ETimeOut, iTimeOut.Int());
	Utils::SaveIntL(iniFile, ELockWhenIdle, iLockWhenIdle);
//App list
	RDictionaryWriteStream stream;
	stream.AssignLC(*iniFile, TUid::Uid(EAppList));//+stream
	stream << (TInt16) iAppList.Count();
	for (TInt i = 0;i < iAppList.Count();i++)
	{
		stream << (TUint32) iAppList[i].iUid;
	}
	stream.CommitL();
	CleanupStack::PopAndDestroy(); //-stream
// Profiles
	stream.AssignLC(*iniFile, TUid::Uid(EProfileList));//+stream
	stream << (TInt16) iProfileList.Count();
	for (TInt i = 0;i < iProfileList.Count();i++)
	{
		stream.WriteUint8L(iProfileList.iData[i]);
	}
	stream.CommitL();
	CleanupStack::PopAndDestroy(); //-stream
//
	iniFile->CommitL();

	CleanupStack::PopAndDestroy();//-iniFile
}

TInt TAutolockSettings::SizeInBytes()
{
	return sizeof(*this) + iAppList.Count()*4 + iProfileList.Count() + 54;
}

void TAutolockSettings::LoadSettingsL(RDesReadStream& aDesStream)
{
	iAutoLockEnabled = aDesStream.ReadInt8L();
	iCustomProfiles = aDesStream.ReadInt8L();
	iLockWhenIdle = aDesStream.ReadInt8L();
	iTimeOut = aDesStream.ReadInt16L();
//
	TInt16 count = aDesStream.ReadInt16L();

	iAppList.Reset();
	for (TInt i = 0;i < count;i++)
	{
		iAppList.Append(TUid::Uid(aDesStream.ReadUint32L()));
	}
	
	count = aDesStream.ReadInt16L();
	iProfileList.iCount = 0;
	for (TInt i = 0;i < count;i++)
	{
		iProfileList.Append(aDesStream.ReadUint8L());
	}
}

void TAutolockSettings::SaveSettingsL(RDesWriteStream& aDesStream)
{
	aDesStream.WriteInt8L( iAutoLockEnabled );
	aDesStream.WriteInt8L( iCustomProfiles );
	aDesStream.WriteInt8L(iLockWhenIdle);
	aDesStream.WriteInt16L(iTimeOut.Int());
//
	TInt16 count = iAppList.Count();
	aDesStream.WriteInt16L(count);


	for (TInt i = 0;i < count;i++)
	{
		aDesStream.WriteUint32L(iAppList[i].iUid);
	}

	aDesStream.CommitL();
//
	count = iProfileList.Count();
	aDesStream.WriteInt16L(count);
//profiles
	for (TInt i = 0;i < count;i++)
	{
		aDesStream.WriteUint8L(iProfileList.iData[i]);
	}

	aDesStream.CommitL();

}

void AddProgramL(RApaLsSession& aLsSession, TUid aAppUid, RArray<TUid>& aAppList)
{
		TApaAppInfo appInfo;
		if (KErrNone == aLsSession.GetAppInfo(appInfo, aAppUid))
		{
			aAppList.Append(aAppUid);
		}
}

void TAutolockSettings::LoadSettingsL()
{
	RFs fs;
	fs.Connect();
	CleanupClosePushL(fs);
	CDictionaryStore* iniFile = CDictionaryFileStore::OpenLC( fs, KSettingsFileName, KUidMyApp );

	Utils::LoadIntL( iniFile, EAutoLockEnabled, iAutoLockEnabled, EFalse );

	//For old settings version support
	if( iAutoLockEnabled == 2 )
		iCustomProfiles = ETrue;
	else
		iCustomProfiles = EFalse;

	Utils::LoadIntL( iniFile, ECustomProfiles, iCustomProfiles, iCustomProfiles );
	
	TInt timeOutInt;
	Utils::LoadIntL(iniFile, ETimeOut, timeOutInt, 40);
	iTimeOut = timeOutInt;
	Utils::LoadIntL(iniFile, ELockWhenIdle, iLockWhenIdle, EFalse);
//Applist
	iAppList.Reset();
	TUid uid = TUid::Uid(EAppList);
	if (iniFile->IsPresentL(uid))
	{
		RDictionaryReadStream stream;
		stream.OpenLC(*iniFile, uid);//+stream
		TInt16 count;
		stream >> count;
		for (TInt i = 0;i < count;i++)
		{
			TUint32 uidVal;
			stream >> uidVal;
			iAppList.Append(TUid::Uid(uidVal));
		}
		CleanupStack::PopAndDestroy();//-stream
	}
	else
	{//load default
		RApaLsSession lsSession;
		lsSession.Connect();
		CleanupClosePushL(lsSession);//+lsSession
	
		AddProgramL(lsSession, TUid::Uid(0x101ff943), iAppList);//radio
		AddProgramL(lsSession, TUid::Uid(0x100058ca), iAppList);//voice recorder
		AddProgramL(lsSession, TUid::Uid(0x06c5b9d2), iAppList);//music player
		
		CleanupStack::PopAndDestroy();//-lsSession
	}
//Profile list
	iProfileList.iCount = 0;
	uid = TUid::Uid(EProfileList);
	if (iniFile->IsPresentL(uid))
	{
		RDictionaryReadStream stream;
		stream.OpenLC(*iniFile, uid);//+stream
		TInt16 count;
		stream >> count;
		for (TInt i = 0;i < count;i++)
		{
			iProfileList.Append(stream.ReadUint8L());
		}
		CleanupStack::PopAndDestroy();//-stream
	}


	CleanupStack::PopAndDestroy(2);//-iniFile
}
